<template>
  <div class="app-container">
    <div
      class="header"
      style="display: flex; justify-content: space-between"
    >
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item>
          <el-button
            icon="refresh"
            color=" rgb(69, 76, 105)"
            style="color: #fff"
            @click="resetQuery()"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleAdd"
            v-hasPermi="['system:car:add']"
            style="padding: 0 20px"
          >
            新 增
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-upload
            ref="upload"
            :action="importUrl"
            :headers="header"
            :on-success="handleSuccess"
            :on-exceed="handleExceed"
            :on-error="handleError"
            :show-file-list="false"
          >
            <el-button
              color="rgb(255,211,146)"
              style="color: rgb(120, 120, 120); padding: 0 20px"
              v-hasPermi="['system:set:import']"
            >
              导 入
            </el-button>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleExport"
            color="rgb(146,226,206)"
            style="color: rgb(120, 120, 120); padding: 0 20px"
            v-hasPermi="['system:set:import']"
          >
            导 出
          </el-button>
        </el-form-item>
      </el-form>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item
          label=""
          prop="belongPlatform"
        >
          <el-select
            style="width: 190px"
            v-model="queryParams.belongPlatform"
            placeholder="请选择所在端"
            clearable
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="(item, index) in allPlatform"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label=""
          prop="contentTitle"
        >
          <el-input
            v-model="queryParams.contentTitle"
            placeholder="请输入内容标题"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="createTime"
        >
          <el-date-picker
            clearable
            v-model="createTime"
            type="datetimerange"
            value-format="YYYY-MM-DD HH:mm:ss"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            @keyup.enter="handleQuery"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="info"
            icon="Search"
            @click="handleQuery()"
          />
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="loading"
      :data="instructionList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="所在端"
        align="center"
        prop="belongPlatform"
      >
        <template #default="scope">
          {{
            allPlatform.find((item) => item.value == scope.row.belongPlatform)
              .label
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="内容标题"
        align="center"
        prop="contentTitle"
      />
      <el-table-column
        label="排序"
        align="center"
        prop="sort"
      />
      <el-table-column
        label="最新更改时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.updateTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:instruction:edit']"
            >编辑</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:instruction:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改使用指南对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="500px"
      append-to-body
      @close="cancel"
    >
      <el-form
        ref="instructionRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="所在端"
          prop="belongPlatform"
        >
          <el-radio-group v-model="form.belongPlatform">
            <el-radio label="0">用户端</el-radio>
            <el-radio label="1">司机端</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="内容标题"
          prop="contentTitle"
        >
          <el-input
            v-model="form.contentTitle"
            placeholder="请输入内容标题"
          />
        </el-form-item>
        <el-form-item
          label="排序"
          prop="sort"
        >
          <el-select
            v-model="form.sort"
            placeholder="请输入排序"
          >
            <el-option
              v-for="item in total"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容详情">
          <editor
            v-model="form.content"
            :min-height="192"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            type="primary"
            @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Instruction">
import { getToken } from "@/utils/auth";
import {
  listInstruction,
  getInstruction,
  delInstruction,
  addInstruction,
  updateInstruction,
} from "@/api/system/instruction";
import useUserStore from "@/store/modules/user";
import { containsProp } from "@vueuse/core";

const { proxy } = getCurrentInstance();

const instructionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

//时间搜索时存储时间参数
const createTime = ref([]);

const header = ref({
  Authorization: "Bearer " + getToken(),
});

const importUrl = ref(
  `${import.meta.env.VITE_APP_BASE_API}/system/instruction/importData`
);

const data = reactive({
  allPlatform: [
    {
      label: "用户端",
      value: 0,
    },
    {
      label: "司机端",
      value: 1,
    },
  ],
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    belongPlatform: null,
    contentTitle: null,
    createTime: null,
  },
  rules: {
    belongPlatform: [
      { required: true, message: "所在端不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules, allPlatform } = toRefs(data);

/** 查询使用指南列表 */
function getList() {
  loading.value = true;
  console.log(queryParams.value,'搜索传递的参数tttttthhh')
  listInstruction(queryParams.value).then((response) => {
    console.log(response,'如果是添加触发getlist,返回的数据有时间吗');
    instructionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  form.value = {};
}

// 表单重置
function reset() {
  form.value = {
    instructionId: null,
    belongPlatform: null,
    contentTitle: null,
    content: null,
    sort: null,
    createTime: null,
    updateTime: null,
  };
  proxy.resetForm("instructionRef");
}

function handleSuccess() {
  getList();
  proxy.$modal.msgSuccess("导入文件上传成功");
}

function handleError(err) {
  proxy.$modal.msgError("上传文件失败:" + err);
}

function handleExceed(file, fileList) {
  this.$refs["upload"].clearFiles();
  this.$refs["upload"].handleStart(file[0]);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;

  //加入的时间搜索
  // console.log(queryParams.value,'搜索时传递的参数')
  if(createTime.value && createTime.value.length >= 2){
    console.log(createTime.value,'搜索时间dfsfdg')
    queryParams.value.startTime = createTime.value[0]
    queryParams.value.endTime = createTime.value[1]
    // queryParams.value.startTime = new Date(queryParams.value.startTime)
    // queryParams.value.endTime = new Date(queryParams.value.endTime)

  }else{
    queryParams.value.startTime = ''
    queryParams.value.endTime = ''
  }
  console.log(queryParams.value,'搜索时传递的参数time');

  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.instructionId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加使用指南";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _instructionId = row.instructionId || ids.value;
  getInstruction(_instructionId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改使用指南";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["instructionRef"].validate((valid) => {
    if (valid) {
      if (form.value.instructionId != null) {
        updateInstruction(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInstruction(form.value).then((response) => {
          console.log(response,'返回的数据包含时间没有');
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _instructionIds = row.instructionId || ids.value;
  proxy.$modal
    .confirm('是否确认删除使用指南编号为"' + _instructionIds + '"的数据项？')
    .then(function () {
      return delInstruction(_instructionIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/instruction/export",
    {
      ...queryParams.value,
    },
    `instruction_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
